<ui:composition template="../layout/layout.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:rich="http://richfaces.org/rich"
	xmlns:a4j="http://richfaces.org/a4j">

	<ui:define name="title">
		<h:outputText value="Sigadoc - Buscar Documento" />
	</ui:define>

	<ui:define name="content">
		<div id="path">
			<h1>Busca Avançada</h1>
			<hr />
		</div>
		<div class="center">
			<h:form >
			<br/>
				<h:outputLabel value="Busca:" for="ibusca" />
				<h:inputText id="ibusca" value="#{buscaMBean.busca}" class="big2"
					watermark="Buscar...">
			
				</h:inputText>
				<br />
				<h:panelGrid columns="4" style="margin-bottom:10px" cellpadding="5">
					<h:outputLabel value="Classe:" for="classe" />
					<h:selectOneMenu id="classe" value="#{buscaMBean.classe}" required="true"
						requiredMessage="* O campo [Classe de documento] é obrigatório!">
						<f:selectItem value="#{buscaMBean.buscaValorPadrao}" noSelectionOption="true"/>
						<f:selectItems value="#{classeDocumentoMBean.listaClasseDocumento}"
							var="#{classe}" itemValue="#{classe.descricao}" />
						<f:converter converterId="simpleEntityConverter" />
					</h:selectOneMenu>

					<h:outputLabel value="Tipo:" for="tipo" />
			<h:selectOneMenu id="tipo" value="#{buscaMBean.tipo}"
				required="true"
				requiredMessage="* O campo [Tipo de documento] é obrigatório!">
				<f:selectItem value="#{buscaMBean.buscaValorPadrao}" noSelectionOption="true"/>
				<f:selectItems value="#{tipoDocumentoMBean.listaTipoDocumento}"
					var="#{tipo}" itemValue="#{tipo.descricao}"  />
				<f:converter converterId="simpleEntityConverter" />
			</h:selectOneMenu>
				</h:panelGrid>
				<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">

				</h:panelGrid>
				<br />
				
				<h:commandButton class="button check" value="Buscar" update="comp" actionListener="#{buscaMBean.actionBuscar}" />
				<h:commandButton class="button clear" value="Limpar"
					action="#{buscaMBean.actionLimpar}" immediate="true">
					<f:ajax render="@form"></f:ajax>
				</h:commandButton>
			</h:form>
		</div>
		
		<div class="spacer"></div>

		<h:form id="comp">
			<h:dataTable styleClass="tabdefault"
				value="#{buscaMBean.listaDocumento}" var="d">
				<h:column>
					<f:facet name="header">
						<h:outputText value="Id" />
					</f:facet>
					<h:outputText value="#{d.id}" />
				</h:column>
				<h:column sortBy="#{d.classe}">
					<f:facet name="header">
						<h:outputText value="Classe" />
					</f:facet>
					<h:outputText value="#{d.classe}" />
				</h:column>
				<h:column sortBy="#{d.tipo}">
					<f:facet name="header">
						<h:outputText value="Tipo" />
					</f:facet>
					<h:outputText value="#{d.tipo}" />
				</h:column>
				<h:column sortBy="#{d.dataCadastro}">
					<f:facet name="header">
						<h:outputText value="Data de Cadastro" />
					</f:facet>
					<h:outputText value="#{d.dataCadastro}" >
						<f:convertDateTime for="dataCadastro" pattern="dd/MM/yyyy" />
					</h:outputText>
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="Metadados" />
					</f:facet>
					<h:outputText value="#{d.listMetavalores}" /> 
				</h:column>
				<h:column sortBy="#{d.dataEntrada}">
					<f:facet name="header">
						<h:outputText value="Data de Entrada" />
					</f:facet>
					<h:outputText value="#{d.dataEntrada}" >
						<f:convertDateTime for="dataEntrada" pattern="dd/MM/yyyy" />
					</h:outputText>
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="Ação" />
					</f:facet>
					<a4j:commandLink value="" title="Download" action="#{buscaMBean.actionDownload}" onclick="#{rich:component('downloadPanel')}.show()"
						render="fileName">
						<h:graphicImage library="images" name="download16x16.png" />
						<f:setPropertyActionListener target="#{buscaMBean.documento}"
							value="#{d}" />
					</a4j:commandLink>
					<h:commandLink value="" action="#{documentoMBean.actionDetalhar(d)}" title="Ver Detalhes" >
						<h:graphicImage library="images" name="ico_det16x16.png" />
					</h:commandLink>
				</h:column>
			</h:dataTable>
		</h:form>
		<rich:popupPanel id="downloadPanel" autosized="true" >
			<f:facet name="header">
				<h:outputText value="Download de Arquivo digital" style="float:left;" />
				<h:graphicImage value="/resources/images/modalclose.png"
					style="cursor:pointer; float:right;"
					onclick="#{rich:component('downloadPanel')}.hide()" />
			</f:facet>
			<h:form>
			<h:outputLabel value="Arquivo: " />
			<h:outputLabel id="fileName" value="#{buscaMBean.fileDownload.name}" />
			<br />
			<h:commandButton value="Download" onclick="#{rich:component('downloadPanel')}.hide()" ajax="false">
				<p:fileDownload value="#{buscaMBean.fileDownload}" />
			</h:commandButton>
			<h:commandButton value="Cancelar" onclick="#{rich:component('downloadPanel')}.hide()" ajax="false" />
			</h:form>
			
		</rich:popupPanel>
	</ui:define>
</ui:composition>